<!-- 批量设置单位服务商 -->
<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="500px"
    append-to-body
    :close-on-click-modal="false"
    @close="closeModal"
  >
    <el-form ref="form" :model="form" :show-message="false" label-width="110px">
      <el-form-item label="审证审方机构" prop="apPrescriptionCompanyId">
        <SelectCompany
          :defaultValue.sync="form.apPrescriptionCompanyId"
          :companyType="0"
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item label="审证审方人" prop="apPrescriptionExpertId">
        <SelectExpert
          :defaultValue.sync="form.apPrescriptionExpertId"
          :comId="form.apPrescriptionCompanyId"
          :prescriptionFlag="1"
          @change="changeExpert"
        />
      </el-form-item>
    </el-form>

    <div class="mt-15 color9 mx-20">
      为单位分配审证审方机构后，审核机构将能看到被分配单位的处方单数据并进行审核。
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">保 存</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import SelectCompany from "@/components/SelectCompany";
import { batchSetCompany } from "@/api/company/company";
import SelectExpert from "@/views/expert/expert/selectExpert.vue";

export default {
  components: {
    SelectCompany,
    SelectExpert
  },
  data() {
    return {
      title: "批量分配审核机构",
      open: false,
      form: {
        batchSetType: 2, // 设置类型：0批量设置服务商 1批量设置跟进人 2批量设置审核机构
        companyIds: [], // 勾选则单位ids
        apPrescriptionCompanyId: null, // 审核机构
        apPrescriptionExpertId: null, // 审核人
      },
    };
  },
  methods: {
    openModal(ids) {
      this.open = true;
      this.form.companyIds = ids;
    },
    closeModal() {
      Object.assign(this.$data, this.$options.data()); // 存在自定义校验规则不能重置data
    },
    submitForm() {
      const that = this;
      batchSetCompany(that.form).then((res) => {
        that.$message.success("批量设置成功");
        that.$emit("refresh");
        that.closeModal();
      });
    },
    // 审证审方人选择
    changeExpert(item) {
      if(item && this.form.apPrescriptionCompanyId !== item.comId){
        this.form.apPrescriptionCompanyId = item.comId;
        setTimeout(() => {
          this.form.apPrescriptionExpertId = item.id;
        }, 100);
      }
    }
  },
};
</script>

<style lang="scss" scoped></style>
